package spider_lib

// 基础包
import (
    "github.com/henrylee2cn/pholcus/app/downloader/request" //必需
    . "github.com/henrylee2cn/pholcus/app/spider" //必需
    "fmt"
    "github.com/PuerkitoBio/goquery"
    "strings"
)

func init() {
    Wnacg.Register()
}

var Wnacg = &Spider{
    Name:        "Wnacg 紳士漫畫",
    Description: "紳士漫畫-專註分享漢化本子&#124;邪惡漫畫",
    // Pausetime: 300,
    // Keyin:   KEYIN,
    // Limit:        LIMIT,
    EnableCookie: false,
    NotDefaultField: true,
    RuleTree: &RuleTree{
        Root: func(ctx *Context) {
            ctx.AddQueue(&request.Request{
                Url:  "http://www.wnacg.org/albums.html",
                Rule: "列表",
                Header: mobileHeader,
            })
        },

        Trunk: map[string]*Rule{
            "列表":{
                ParseFunc: func(ctx *Context) {
                    count := ToNum(ctx.GetDom().Find(".paginator").Find(".count").Text())
                    for i := 1; i <= count; i++ {
                        ctx.AddQueue(&request.Request{
                            Url:  fmt.Sprintf("http://www.wnacg.org/albums-index-page-%d.html", i),
                            Rule: "获取",
                            Header: mobileHeader,
                        })
                    }
                },
            },

            "获取":{
                ItemFields: []string{
                    "aid",
                    "title",
                    "pages",
                    "cover",
                },
                ParseFunc: func(ctx *Context) {
                    ctx.GetDom().Find(".con_mod a").Each(func(i int, s *goquery.Selection) {
                        href, _ := s.Attr("href")
                        title := s.Find(".title_name").Text()
                        pages := ToNum(s.Find(".col_99").Eq(0).Text())
                        cover, _ := s.Find("img").Attr("src")
                        aid := ToNum(href)
                        ctx.Output(map[int]interface{}{
                            0:aid,
                            1:title,
                            2:pages,
                            3:cover,
                        })
                        //ctx.AddQueue(&request.Request{
                        //    Url:  fmt.Sprintf("http://m.wnacg.net/photos-gallery-aid-%d.html", aid),
                        //    Rule: "图片地址",
                        //    Header: mobileHeader,
                        //    Temp: map[string]interface{}{"aid": aid},
                        //})
                    })
                },
            },

            "图片地址":{
                ItemFields: []string{
                    "aid",
                    "url",
                    "url2",
                },
                ParseFunc: func(ctx *Context) {
                    var aid int
                    ctx.GetTemp("aid", &aid)
                    urls := url_re.FindAllString(ctx.GetDom().Text(), -1)
                    for _, url := range urls[:len(urls) - 1] {
                        url = url[7:len(url) - 1]
                        url = url[strings.Index(url, "/"):]
                        url2 := strings.Replace(url, "/m/", "/data/", 1)
                        ctx.Output(map[int]interface{}{
                            0:aid,
                            1:url,
                            2:url2,
                        })
                    }
                },
            },

        },
    },
}

